package com.sa.web.behind.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.QaBean;
import com.sa.web.dao.service.QaDao;
import com.sa.web.util.Page;
import com.sa.web.util.Util;

public class QaManageAction extends ActionSupport {
	/** 序列 */
	private static final long serialVersionUID = 1L;
	/** 设置Qa的Dao对象 */
	QaDao qaDao;
	/** 实例QaBean */
	QaBean qaBean;
	/** 设置Qa集合 */
	List<QaBean> qaList;
	/** easyUi对象*/
	private JSONArray rows;
	/** 信息总数*/
	private JSONArray total;
	/** 前台信息*/
	private JSONArray msg;
    
	/**
     * 留学问答初始化页面
     * @return success
     * @throws Exception
     */
	public String initQaFind() throws Exception {
		return "success";
	}
	
	/**
     * 留学问答增加页面初始化
     * @return success
     * @throws Exception
     */
	public String initQaAdd() throws Exception {
		return SUCCESS;
	}

    /** 
     * 留学问答查询
     * @return JSON
     * @throws Exception
     */
	public String findQaList()  throws Exception{
		 qaList = qaDao.getQaInfo(Page.getStarIndex(),Page.getPreIndex());
		 rows = JSONArray.fromObject(qaList);
		 total = JSONArray.fromObject(qaDao.getQaCount());
		 return Util.jSONSuccess();
	}
	/**
	 * 增加留学问答信息
	 * @return succes
	 * @throws Exception
	 */
	public String addQaInfo() throws Exception {
		qaBean.setQaId(Util.getUUID());
		if(StringUtils.isEmpty(qaBean.getQuContext())){
			msg = Util.returnMsg("问题内容不能为空，请重新输入");
		}
		else if(StringUtils.isEmpty(qaBean.getAnContext())){
			msg = Util.returnMsg("答案内容不能为空，请重新输入");			
		}
		else if(qaBean.getAnContext().length()>500){
			msg = Util.returnMsg("答案的长度不能大于500字，请重新输入");
		}
		else{
		boolean result=qaDao.addQaInfo(qaBean);
		msg = result?Util.returnMsg("添加成功"):Util.returnMsg("添加失败");
		}
		return Util.jSONSuccess();
	}
	/** 
	 * 删除留学问答
	 * @return json
	 * @throws Exception
	 */
	public String deleteQaInfo() throws Exception {
		qaDao.deleteQaInfo(Util.getRequest().getParameter("qaId")==null?"":Util.getRequest().getParameter("qaId").toString());
		 msg = JSONArray.fromObject("[\"删除成功\"]");
		return Util.jSONSuccess();
	}

/**************************getter and setter****************************************************************************************************/
	public QaDao getQaDao() {
		return qaDao;
	}

	public void setQaDao(QaDao qaDao) {
		this.qaDao = qaDao;
	}

	public QaBean getQaBean() {
		return qaBean;
	}

	public void setQaBean(QaBean qaBean) {
		this.qaBean = qaBean;
	}

	public List<QaBean> getQaList() {
		return qaList;
	}

	public void setQaList(List<QaBean> qaList) {
		this.qaList = qaList;
	}
    /**
     * 无get方法无法到页面
     * @return JSONArray
     */
	public JSONArray getRows() {
		return rows;
	}
    /**
     * 无get方法无法到页面
     * @return JSONArray
     */
	public JSONArray getTotal() {
		return total;
	}
    /**
     * 无get方法无法到页面
     * @return JSONArray
     */
	public JSONArray getMsg() {
		return msg;
	}
}
